package ${cfg.rootPath}.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import ${cfg.rootPath}.entity.${entity};
import ${cfg.rootPath}.data.${entity}Data;
import ${cfg.rootPath}.mapper.${entity}Mapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
*
* @author ${author}
* @since ${date}
*/
@Slf4j
@Component
public class ${entity}Service extends ServiceImpl<${table.mapperName}, ${entity}> {

    public ID insertModel(${entity}Data data) {
        ${entity} entity = ConvertHelper.convert(data, ${entity}.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(${entity}Data data) {
        ${entity} entity = ConvertHelper.convert(data, ${entity}.class);
        return update(entity, new UpdateWrapper<${entity}>().eq(Table.ID, data.getId()));
    }

    public ${entity}Data selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<${entity}>().eq(Table.ID, data.getId())), ${entity}Data.class);
    }

    public List<${entity}Data> selectByData(${entity}Data data) {
        LambdaQueryWrapper<${entity}> query = new LambdaQueryWrapper<>();
        query.eq(!ObjectUtil.isEmpty(data.getId()), ${entity}::getId, data.getId());
        query.orderByDesc(${entity}::getId);
        return ConvertHelper.convert(list(query), ${entity}Data.class);
    }

    public PageList<${entity}Data> selectByPage(PageQueryDto<${entity}Data> pageData) {

        ${entity}Data data = pageData.getData();
        LambdaQueryWrapper<${entity}> query = new LambdaQueryWrapper<>();
        query.eq(!ObjectUtil.isEmpty(data.getId()), ${entity}::getId, data.getId());
        query.orderByAsc(${entity}::getId);

        return PageHelper.pageList(page(PageHelper.page(pageData), query), ${entity}Data.class);
    }

}
